#!/usr/bin/python

import pickle

# Open file in binary mode
with open('mydata.pickle', 'wb') as mysavedata:
    # Save data with dump
    pickle.dump([1,2,'three'], mysavedata)

with open('mydata.pickle', 'rb') as myreaddata:
    # Recover data using load
    list = pickle.load(myreaddata)

    print list


print 'example 1'
t1 = ('this is a string', 42, [1,2,3], None)

# Returns a string containing an object in pickle format
p1 = pickle.dumps(t1)
print p1


print 'example 2'
# Returns an object that is contained in a pickle string, that is, the
# data before persistent storage
t2 = pickle.loads(p1)
print t2

print 'example 3'
# Both have an optional argument, if True, then the parameter specifies a 
# faster and smaller binary representation to create the pickle
p2 = pickle.dumps(t1, True)
print p2

print "example 4"
t3 = pickle.loads(p2)
print t3


print 'example 5'
a1 = 'apple'
b1 = {1:'one', 2:'two', 3:'three'}
c1 = ['fee', 'fie', 'foe', 'fun']
f1 = file('mysavedata2.pk1', 'wb')

pickle.dump(a1, f1, True)
pickle.dump(b1, f1, True)
pickle.dump(c1, f1, True)

f1.close()

f2 = file('mysavedata2.pk1', 'rb')
a2 = pickle.load(f2)
b2 = pickle.load(f2)
c2 = pickle.load(f2)

print a2
print b2
print c2

f2.close()
